#include"head.h"
int signin_user(int socket, int *success){
TLV*TLV_T=(TLV*)malloc(sizeof(TLV));
TLV*TLV_RECV=(TLV*)malloc(sizeof(TLV));
memset(TLV_RECV,0,sizeof(TLV));

memset(TLV_T,0,sizeof(TLV));
TLV_T->type=CMD_SIGNIN;
 printf("要求：输入的用户名和密码均为8位\n");
printf("登录开始，请输入用户名\n");

char buf[9];
char buf1[9];
memset(buf,0,9);
memset(buf1,0,9);
char c;
while ((c = getchar()) != '\n' && c != EOF);
if (fgets(buf, sizeof(buf), stdin)) {
    buf[strcspn(buf, "\n")] = '\0';
    printf("你输入的用户名是%s\n", buf);
} else {
    printf("读取输入失败\n");
}
buf[8]='\0';
int num_username=strlen(buf);
if(num_username!=8){
printf("用户名不足八位\n");
return -1;
}
TLV_T->length=16;
printf("准备发送密码\n");
 while ((c = getchar()) != '\n' && c != EOF);
if (fgets(buf1, sizeof(buf1), stdin)) {
    // 去除可能的换行符
    buf1[8] = '\0'; 
    printf("你输入的密码是%s\n", buf1);
} else {
    printf("读取输入失败\n");
}

       for (int i = 0; i < 8; i++) {
        TLV_T->value[i] = buf[i];
        TLV_T->value[i + 8] = buf1[i];
    }
     printf("TLV_T->value=%s\n",TLV_T->value);
    

    sendTlv(socket,TLV_T);
recvTlv(socket,TLV_RECV);
if(TLV_RECV->type==0xFF){
printf("登录失败,%s\n",TLV_RECV->value);
}
else{
printf("success\n");
*success= 1;
}
free(TLV_RECV);
free(TLV_T);
return (*success);
};

